<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\db;

class Index extends Controller
{
    public function index()
    {
        if(session('uid')){
            return view("Admin/index");
        }
        return view("Index/login");
    }
   
    // 消息回调
    public function msgreturn(){
        file_put_contents('msg.log',$_POST, FILE_APPEND);
        file_put_contents('msg2.log',$_GET);
    }
    // 获取推广h5数据
    public function getHotmsg(){
        header('Access-Control-Allow-Origin:*');  
   // 响应类型  
        header('Access-Control-Allow-Methods:*');  
   // 响应头设置  
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        $data['count'] = db('Users')->count();
        $data['teizi'] = db('user_advertisement')->count();
        $data['hotcount'] = db('user_advertisement')->sum('click_num');
        $data['title'] = "【商圈】全国商超食品，酒水采购圈！";
        $data['desc'] = "无论是便利店，商超， 出租出兑。亦或者寻找店面的各种信息都可在本群 社区发布。加入本群后，便利店、商超等可实现无缝对接、资源共享。每个用户要求商户认证,保证一人一号，杜绝商业诈骗。
        ";
        $mentlist = db('user_advertisement')->order('click_num desc')->limit(50)->select();
        foreach ($mentlist as $key=>$val){
            $map['id'] = $val['user_id'];
            $info = db('Users')->where($map)->find();
            $mentlist[$key]['name'] = $info['nickname'];
            $mentlist[$key]['phone'] = "手机：". substr($info['phone'],0,3)."********";
            $mentlist[$key]['img'] = str_replace("http:","https:",$info['portraitUri'] ); 
            $mentlist[$key]['content2'] = emoji2str($val['content']);
            $mentlist[$key]['content'] = emoji2str($val['content']);
        }
        $data['mentlist'] = $mentlist;
        echo json_encode(array('status' => 1,'info' =>$data));exit;
    }
    public function search(){
        header('Access-Control-Allow-Origin:*');  
   // 响应类型  
        header('Access-Control-Allow-Methods:*');  
   // 响应头设置  
        header('Access-Control-Allow-Headers:x-requested-with,content-type');
        
        $like = input('keyword');
        $mentlist = db('user_advertisement')->where('content','like',"%{$like}%")->limit(50)->select();
        foreach ($mentlist as $key=>$val){
            $map['id'] = $val['user_id'];
            $info = db('Users')->where($map)->find();
            $mentlist[$key]['name'] = $info['nickname'];
            $mentlist[$key]['phone'] = "手机：". substr($info['phone'],0,3)."********";
            $mentlist[$key]['img'] = str_replace("http:","https:",$info['portraitUri'] ); 
            $mentlist[$key]['content2'] = emoji2str($val['content']);
            $mentlist[$key]['content'] = emoji2str($val['content']);
        }
        $data['mentlist'] = $mentlist;
        echo json_encode(array('status' => 1,'info' =>$data));exit;
    }
    // 获取消息列表
    public function getrongyunMsg(){
        $timestamp = time();

        $nonce = rand();
        
        $appSecret = 'rjO7mV5y5BNJny';
        
        $signature = sha1($appSecret.$nonce.$timestamp);
        $date = date('Ymd');
        
         $header  = array(
            'App-Key:'."y745wfm8ydvjv",
            'Nonce:'.$nonce,
            'Timestamp:'.$timestamp,
            'Signature:'.$signature
        );
        $url = "https://api.developer.rongcloud.cn/stat/imCensor?date=20211105";
       
        $data = [];
        $r = $this->_curlPost($url,$data,$header);
        print_r($r);
    }
    public function _curlPost($url, $data, $header = [])
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $info = curl_exec($ch);
        if (curl_errno($ch)) {
            echo 'Errno' . curl_error($ch);
        }
        curl_close($ch);
        return $info;
    }
    public function help(){
        return view("Index/help");
    }
    // 获取文章标题
    public function getWenzhangTitle(){
       $info = db('agreement_explain')->where('explain_type',5)->select();
       echo_info(0,'查询成功',count($info),$info);
    }
    // 帮助中心详情
    public function detail(){
        return view("Index/detail");
    }
    // 帮助中心
    public function getDetail(){
        $id = input('post.id');
        $info = db('agreement_explain')->where('id',$id)->find();
        echo_info(0,'查询成功',count($info),$info);
    }
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
    public function login(){
        if(request()->isPost()){

            $where['username'] =input('post.username');
            $where['role'] = 1;
            $res = db('admin')->where($where)->find();

            if($res){
                if($res['password'] == md5(input('post.password'))){
                    session('uid',$res['id']);
                    $ip = $_SERVER["REMOTE_ADDR"];; //登录IP
                    $update['logTime'] = date('Y-m-d H:i:s');
                    $update['log_ip'] = $ip;
                    $update['last_ip'] = $ip;
                    $where2['id'] = $res['id'];
                    db('admin')->where($where2)->update($update);
                    $this->success("登录成功");
                }else{
                    $this->error("登录失败");
                }
            }else{
                $this->error("登录用户存在");
            }

        }
        return view("Index/login");
    }
    // 注册协议和隐私政策
    public function xieyi1(){
        return view("Index/xieyi1");
    }
    public function getXieyi1(){
        $where['id'] =input('post.id');
        $info = db('agreement_explain')->where($where)->find();
        echo_info(0,'查询成功',count($info),$info);
    }
    // 随机获取群头像
    public function getImg(){
        $avatar = list_file('./avatar');
        $img = $avatar[rand(0,count($avatar)-1)];
        $url = "https://".$_SERVER['SERVER_NAME'].$img;
        echo $url;
    }
    public function gather()
    {
        $arrContextOptions = ["ssl" => ["verify_peer" => false, "verify_peer_name" => false,],];
        $response          = file_get_contents("https://filfox.info/zh/address/f0463030", false, stream_context_create($arrContextOptions));

        $rules             = [
            // 标题
            'title'   => ['.items-center p.text-sm', 'text'],
            // 内容
            'content' => ['.items-center span.mx-1', 'text'],
        ];
        $url = "https://www.filscout.com/zh/miner/f0463030";
        $contents = file_get_contents($url);
        print_r($contents);die;
        $startindex = strpos($contents,"出块数量");

        $endindex = strpos($response,"<\/span>", $startindex);
        $data['chu_num'] = substr($response, $startindex,8);
        echo $data['chu_num'];
        foreach ($hj as $k => $v) {
            if($k == 3){ // 原值算力
                $indexs = strpos($v['title'],":");
                $len = strlen($v['title']);
                $data['suanli'] = substr($v['title'],$indexs+1,$len-$indexs-1);
            }
            if($k==4){ // 累计出块奖励
                $indexs = strpos($v['title'],":");
                $len = strlen($v['title']);
                $data['leiji_chukuai'] = substr($v['title'],$indexs+1,$len-$indexs-1);
            }
            if($k == 5){ // 扇区大小
                $indexs = strpos($v['title'],":");
                $len = strlen($v['title']);
                $data['shanqu'] =  substr($v['title'],$indexs+1,$len-$indexs-1);
            }
            if($k==9){ // 存储服务效率
                //$indexs = strpos($v['title'],":");
                //$len = strlen($v['title']);
                //$data['fuwu_lv'] = substr($v['title'],$indexs+1,$len-$indexs-1);
            }
            if($k == 26){ // 出块数量
                $indexs = strpos($v['content'],":");
                $len = strlen($v['content']);
                $data['chu_num'] = substr($v['content'],$indexs+1,$len-$indexs-1);
            }

            if($k == 19){ // 累计出块份数
                $indexs = strpos($v['content'],":");
                $len = strlen($v['content']);
                $data['fenshu'] = substr($v['content'],$indexs+1,$len-$indexs-1);
            }
            if($k == 27){ // 出块份数
                $indexs = strpos($v['content'],":");
                $len = strlen($v['content']);
                $data['chu_fenshu'] = substr($v['content'],$indexs+1,$len-$indexs-1);
            }
        }

       // db('tw_config')->where('id',1)->update($data);
        echo "更新成功矿工概括".date('Ymd-H:i:s');

       // $this->gather2();
    }
    public function gather2()
    {
        sleep(3);
        $arrContextOptions = ["ssl" => ["verify_peer" => false, "verify_peer_name" => false,],];
        $response          = file_get_contents("https://filfox.info/zh?standalone=true", false, stream_context_create($arrContextOptions));
        $rules             = [
            // 标题
            'title'   => ['.text-left span.align-middle', 'text'],
            // 内容
            'content' => ['.text-left.text-sm.items-start', 'text'],
        ];
        $hj                = (new \QL\QueryList())->Query($response, $rules)->data;
        foreach ($hj as $k => $v) {

            if ($v['title'] === "24h平均提供存储服务收益") {
                $data['coin_count'] = str_replace(' FIL/TiB','',$hj[$k-3]['content']);
            }
            if ($v['title'] === "最新区块时间") {
                $data['xchday_num'] = $v['content'];
            }

            if ($v['title'] === "全网有效算力") {
                $data['addr_num'] = $v['content'];
            }
            if ($v['title'] === "区块高度") {
                $data['web_num'] = $v['content'];
            }
            if ($v['title'] === "当前全网有效算力（有效存储空间）的总和") {
                $data['coin_count'] = $v['content'];
            }
            if ($v['title'] === "每区块奖励") {
                $data['xchhour_num'] = $v['content'];
            }

        }

        db('tw_config')->where('id',1)->update($data);
        echo "更新成功平台概括".date('Ymd-H:i:s');

    }

}
